package com.dmlll.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmlll.VO.DrugMessageVO;
import com.dmlll.VO.HospitalPurchaseCatalogVO;
import com.dmlll.common.DataGridView;
import com.dmlll.domain.*;
import com.dmlll.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dmLll
 * @since 2021-03-31
 */
@RestController
@RequestMapping("hospitalPurchaseCatalog")
public class HospitalPurchaseCatalogController {
    @Autowired
    private HospitalPurchaseCatalogService hospitalPurchaseCatalogService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private DrugMessageService drugMessageService;
    @Autowired
    private ProviderDrugCatalogControlService providerDrugCatalogControlService;
    /**
     * 获取有效的目录信息（药品交易状态正常、供应商供货状态正常、采购单中不存在）
     * @param hospitalPurchaseCatalogVO
     * @return
     */
    @RequestMapping("getAvaCatalog")
    public DataGridView getAvaCatalog(HospitalPurchaseCatalogVO hospitalPurchaseCatalogVO, DrugMessageVO drugMessageVO, Provider provider, Integer controlState){
        IPage<HospitalPurchaseCatalog> iPage = new Page<>(hospitalPurchaseCatalogVO.getPage(),hospitalPurchaseCatalogVO.getLimit());
        QueryWrapper<HospitalPurchaseCatalog> queryWrapper = new QueryWrapper<>();
        List<HospitalPurchaseCatalog> list = hospitalPurchaseCatalogService.getAvaCatalog(iPage,hospitalPurchaseCatalogVO.getDrugMessageIds(),
                hospitalPurchaseCatalogVO.getHospitalId(),drugMessageVO,provider,controlState);
        //获取所有的有效数据
        for (HospitalPurchaseCatalog catalog:list){
            //填充药品
            if(catalog.getDrugMessageId()!=null)
                catalog.setDrugMessage(drugMessageService.getById(catalog.getDrugMessageId()));
            //填充医院
            if(catalog.getHospitalId()!=null)
                catalog.setHospital(hospitalService.getById(catalog.getHospitalId()));
            //填充供应商
            if(catalog.getProviderId()!=null)
                catalog.setProvider(providerService.getById(catalog.getProviderId()));
            //
            if(catalog.getProviderId()!=null&&catalog.getDrugMessageId()!=null){
                QueryWrapper<ProviderDrugCatalogControl> wrapper = new QueryWrapper<>();
                wrapper.eq("provider_id",catalog.getProviderId());
                wrapper.eq("drug_message_id",catalog.getDrugMessageId());
                catalog.setProviderDrugCatalogControl(providerDrugCatalogControlService.getOne(wrapper));
            }
        }
        //筛选

        iPage.setRecords(list);
        return new DataGridView(iPage.getTotal(),iPage.getRecords());
    }
}

